//
//  SideMenuView.swift
//  Twitter
//
//  Created by Hoa.wang on 2022/3/30.
//

import SwiftUI
import Kingfisher

struct SideMenuView: View {
    
    @EnvironmentObject var authViewModel: AuthViewModel
    
    var body: some View {
        if let user = authViewModel.currentUser {
            VStack(alignment: .leading, spacing: 32) {
                VStack(alignment: .leading) {
                    if let profileURL = user.profileImage?.url {
                        KFImage(profileURL)
                            .resizable()
                            .scaledToFill()
                            .clipShape(Circle())
                            .frame(width: 48, height: 48)
                    }
                    
                    VStack(alignment: .leading, spacing: 4) {
                        Text(user.fullname ?? "")
                            .font(.headline)
                        
                        Text("@\(user.username!)")
                            .font(.caption)
                            .foregroundColor(.gray)
                    }
                    
                    UserStatsView()
                        .padding(.vertical)
                }
                .padding(.leading)
                
                ForEach(SideMenuViewModel.allCases, id: \.rawValue) { item in
                    if item == .profile {
                        NavigationLink {
//                            ProfileView(user: user)
                        } label:  {
                            OptionView(sideMenuViewModel: item)
                        }
                    }
                    else if item == .logout {
                        Button {
                            authViewModel.signOut()
                        } label: {
                            OptionView(sideMenuViewModel: item)
                        }
                    }
                    else {
                        OptionView(sideMenuViewModel: item)
                    }
                }
                .padding(.vertical, 4)

                Spacer()
            }
        }
    }
}


struct SideMenuView_Previews: PreviewProvider {
    static var previews: some View {
        SideMenuView()
    }
}
